package com.fy56.platform.logistics.utils;

import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author  by Edwin Yang on 2018/3/14 0014.
 */
public class HttpUtil {

    public static String postWidthParamName(String url, String paramName, Map<String, Object> postParams){
        final Map<String, String> map = Maps.newHashMap();
        map.put(paramName, JSONObject.toJSONString(postParams));
        return post(url,map);
    }

    public static String post(String url, Map<String, String> postParams){
        HttpRequest httpRequest = HttpRequest.post( url,postParams,true);
        httpRequest.headers(setHeader());
        return httpRequest.body("utf-8");
    }

    public static String get(String url, Map<String, String> postParams){
        HttpRequest httpRequest = HttpRequest.get( url,postParams,true);
        httpRequest.headers(setHeader());
        return httpRequest.body("utf-8");
    }

    public static String get(String url){
        return get(url,null);
    }

    /**
     * 设置httpHeader
     */
    public static Map<String, String> setHeader(){
        Map<String, String> header = Maps.newHashMap();
        header.put("Authorization", "ignore");
        header.put("Content-type", "application/x-www-form-urlencoded");
        header.put("charset", "utf-8");
        return  header;
    }
}
